// app.js
const Koa = require('koa')
const app = new Koa()
const config = require('./config/index')
const router = require('./routes/index')

// koa-logger
const logger = require("koa-logger")
const moment = require("moment")
app.use(logger((str) => {
  console.log(moment().format('YYYY-MM-DD HH:mm:ss') + str);
}))
// koa-static
const static = require('koa-static')
app.use(static(__dirname + '/public'))
// koa-views
const views = require('koa-views')
const path = require('path')
app.use(views(path.join(__dirname + '/views'), {
  map: {
    html: 'ejs'
  }
}))
// koa-swagger
const KoaSwagger = require('koa2-swagger-ui')
app.use(KoaSwagger({
  routePrefix: '/swagger',
  swaggerOptions: {
    url: 'swagger.json'
  }
}))
const swagger = require('./util/swagger')
app.use(swagger.routes(), swagger.allowedMethods())


app.use(router.routes(), router.allowedMethods())


app.use(async ctx => {
 ctx.body = '哈哈哈哈'
})
 
// 监听端口
app.listen(config.port, () => {
  console.log(`Server running on http://localhost:${config.port || 3000}`)
})
// 项目启动错误错误监听
app.on('error', (ctx, error) => {
  // 项目启动错误
  ctx.body = error
})

